home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Win95 Secrets / SETUP.Z / RETURN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  2.8 KB  |  114 lines

  1. //==================================
  2. // APISPY32 - Matt Pietrek 1995
  3. // FILE: RETURN.C
  4. //==================================
  5. #include <windows.h>
  6. #include <malloc.h>
  7. #include "perthred.h"
  8. #include "return.h"
  9. #include "log.h"
  10.  
  11. void AsmCommonReturnPoint(void);
  12.  
  13. DWORD TlsIndex = 0xFFFFFFFF;
  14.  
  15. BOOL InitThreadReturnStack(void)
  16. {
  17.     PPER_THREAD_DATA pPerThreadData;
  18.     
  19.     static BOOL firstTime = TRUE;
  20.     
  21.     if ( firstTime )
  22.     {
  23.         TlsIndex = TlsAlloc();
  24.         firstTime = FALSE;
  25.     }
  26.  
  27.     if ( TlsIndex == 0xFFFFFFFF )
  28.         return FALSE;
  29.     
  30.     pPerThreadData = malloc( sizeof(PER_THREAD_DATA) );
  31.     if ( !pPerThreadData )
  32.         return FALSE;
  33.     
  34.     pPerThreadData->FunctionStackPtr = 0;
  35.     
  36.     TlsSetValue(TlsIndex, pPerThreadData);
  37.     
  38.     return TRUE;
  39. }
  40.  
  41. BOOL ShutdownThreadReturnStack(void)
  42. {
  43.     PPER_THREAD_DATA pPerThreadData;
  44.     
  45.     if ( TlsIndex == 0xFFFFFFFF )
  46.         return FALSE;
  47.     
  48.     pPerThreadData = TlsGetValue( TlsIndex );
  49.     if ( pPerThreadData )
  50.         free( pPerThreadData );
  51.     
  52.     return TRUE;
  53. }
  54.  
  55. BOOL InterceptFunctionReturn(PSTR pszName, PDWORD pFrame)
  56. {
  57.     PPER_THREAD_DATA pStack;
  58.     DWORD i;
  59.     
  60.     pStack = (PPER_THREAD_DATA)TlsGetValue(TlsIndex);
  61.     if ( !pStack )
  62.         return FALSE;
  63.  
  64.     if ( pStack->FunctionStackPtr >= (MAX_HOOKED_FUNCTIONS-1) )
  65.         return FALSE;
  66.     
  67.     i = pStack->FunctionStackPtr;
  68.     
  69.     pStack->FunctionStack[i].pfnReturnAddress = (PVOID)pFrame[0];
  70.     pStack->FunctionStack[i].pszName = pszName;
  71.     pStack->FunctionStackPtr++;
  72.     
  73.     pFrame[0] = (DWORD)AsmCommonReturnPoint;
  74.     
  75.     return TRUE;
  76. }
  77.  
  78. // return_address <- pFrame[8]
  79. // EAX            <- pFrame[7]
  80. // ECX            <- pFrame[6]
  81. // EDX            <- pFrame[5]
  82. // EBX            <- pFrame[4]
  83. // ESP            <- pFrame[3]
  84. // EBP            <- pFrame[2]
  85. // ESI            <- pFrame[1]
  86. // EDI            <- pFrame[0]
  87.  
  88. //
  89. // Common return point for all functions that we've intercepted.
  90. // Called by _AsmCommonReturnPoint in ASMRETRN.ASM
  91. // pFrame is a pointer to the stack frame set up by the PUSHAD
  92. // (see above comment for the layout of this frame)
  93. //
  94. void CCommonReturnPoint( PDWORD pFrame )
  95. {
  96.     PPER_THREAD_DATA pStack;
  97.     DWORD i;
  98.  
  99.     // Get the function stack for the current thread
  100.     pStack = (PPER_THREAD_DATA)TlsGetValue(TlsIndex);
  101.     if ( !pStack )
  102.         return;
  103.  
  104.     i = --pStack->FunctionStackPtr;
  105.  
  106.     // Emit the information about the function return value to the logging
  107.     // mechanism.
  108.     LogReturn(pStack->FunctionStack[i].pszName, pFrame[7], i);
  109.  
  110.     // Patch the return address back to what it was when the function
  111.     // was originally called.
  112.     pFrame[8] = (DWORD)pStack->FunctionStack[i].pfnReturnAddress;
  113. }
  114.